/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import otros.Fecha;
import otros.Util;
import tablasSql.Articulos;
import tablasSql.Familias;
import tablasSql.RegStock;
import tablasSql.Sql;
import tablasSql.Stock;


/**
 *
 * @author FERRETERIA
 */
public class FArticulo extends javax.swing.JInternalFrame {
    private boolean guardar;

    /**
     * Creates new form FArticulo
     */
    public FArticulo() {
        init();
        guardar = true;
        reset();
        setActivarCampos(true);
    }
    
    public FArticulo(String codigo) {
        init();
        guardar = false;
        escribirCampos(new Articulos().getArticulo(codigo));            
    }
    
    public boolean hasArticulo() {
        if (txtId.getText().compareTo("") != 0)
            return true;
        else
            return false;
    }
    
    public void setActivarCampos(boolean edit) {
        //txtId.setEnabled(edit);
        txtCodAsociado.setEnabled(edit);
        txtCodBarras1.setEnabled(edit);
        txtCodBarras2.setEnabled(edit);
        //txtCodigo.setEnabled(edit);
        txtCosto.setEnabled(edit);
        txtDetalle.setEnabled(edit);
        txtMargen2.setEnabled(edit);
        txtPrecioVenta.setEnabled(edit);
        txtLocalizacion.setEnabled(edit);
        txtIgic.setEnabled(edit);
        cbFamilia.setEnabled(edit);
        bEditar.setEnabled(!edit);
        bBorrar.setEnabled(false);
        bGuardar.setEnabled(false);
        if (edit)
            bGuardar.setEnabled(true);
        else
            bBorrar.setEnabled(true);       
    }
    
    public void escribirCampos (ResultSet art) {
        try {
            art.next();
            txtId.setText(String.valueOf(art.getInt("id")));
            txtCodAsociado.setText(art.getString("CodigoAsociado"));
            txtCodBarras1.setText(art.getString("CodigoBarras1"));
            txtCodBarras2.setText(art.getString("CodigoBarras2"));
            txtCodigo.setText(art.getString("Codigo"));
            txtCosto.setValue(art.getFloat("Costo"));
            txtDetalle.setText(art.getString("Detalle"));
            txtMargen2.setText(String.valueOf(art.getFloat("Margen")) + " %");
            txtPrecioVenta.setValue(art.getFloat("PrecioVenta"));
            txtLocalizacion.setText(art.getString("Localizacion"));
            txtIgic.setValue(art.getFloat("igic"));
            String familia = Familias.getNombreFamilia(String.valueOf(art.getInt("IdFamilia")));
            cbFamilia.setSelectedItem(familia);
            art.close();
        } catch (SQLException ex) {
            System.out.println("Familia.escribirCampos " + ex);
        } 
    }
    
    public void init () {
        initComponents();
        setActivarCampos(false);
        this.setClosable(true);

        // Formato de visualización
        NumberFormat dispFormat = NumberFormat.getCurrencyInstance();
        // Formato de edición: inglés (separador decimal: el punto)
        NumberFormat editFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
        // Para la edición, no queremos separadores de millares
        editFormat.setGroupingUsed(false);
        // Creamos los formateadores de números
        NumberFormatter dnFormat = new NumberFormatter(dispFormat);
        NumberFormatter enFormat = new NumberFormatter(editFormat);
        // Creamos la factoría de formateadores especificando los
        // formateadores por defecto, de visualización y de edición
        DefaultFormatterFactory currFactory = new DefaultFormatterFactory(dnFormat, dnFormat, enFormat);
        // El formateador de edición admite caracteres incorrectos
        enFormat.setAllowsInvalid(true);
        // Asignamos la factoría al campo
        txtPrecioVenta.setFormatterFactory(currFactory);
        
        dispFormat = new DecimalFormat ("#0.0000 €");
            // Formato de edición: inglés (separador decimal: el punto)
        DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols();
        unusualSymbols.setDecimalSeparator('.');
        editFormat = new DecimalFormat ("#0.0000", unusualSymbols);
        editFormat.setGroupingUsed(false);
        dnFormat = new NumberFormatter(dispFormat);
        enFormat = new NumberFormatter(editFormat);
        enFormat.setAllowsInvalid(true);
        currFactory = new DefaultFormatterFactory(dnFormat, dnFormat, enFormat);
        txtCosto.setFormatterFactory(currFactory);
        
        txtPrecioVenta.setValue(0);
        txtCosto.setValue(0);
        
        Object [][] familias = Familias.getFamilias("");
        for (int i = 0; i < familias.length; i++)
            cbFamilia.addItem(familias[i][2]);
    }
    
    private void reset() {
        txtId.setText(String.valueOf(Sql.getNextId("narticulos")));
        txtCodigo.setText(""+((Integer.parseInt(Articulos.getMaxCodigo())+1)));
        txtCodBarras1.setText("0");
        txtCodBarras2.setText("0");
        txtCodAsociado.setText("0");
        txtCosto.setValue(0);
        txtDetalle.setText("");
        txtMargen2.setText("0%");
        txtPrecioVenta.setValue(0);
        txtLocalizacion.setText("");
        txtIgic.setValue(0);
        cbFamilia.setSelectedItem("Ninguno");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtCodigo = new javax.swing.JTextField();
        txtCodBarras1 = new javax.swing.JTextField();
        txtCodAsociado = new javax.swing.JTextField();
        txtDetalle = new javax.swing.JTextField();
        txtCodBarras2 = new javax.swing.JTextField();
        cbFamilia = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        bGuardar = new javax.swing.JButton();
        bEditar = new javax.swing.JButton();
        bBorrar = new javax.swing.JButton();
        txtCosto = new javax.swing.JFormattedTextField();
        txtPrecioVenta = new javax.swing.JFormattedTextField();
        txtMargen2 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtLocalizacion = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtIgic = new javax.swing.JFormattedTextField();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar un Articulo");
        setEnabled(false);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Id");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 54;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(20, 31, 0, 0);
        getContentPane().add(jLabel1, gridBagConstraints);

        jLabel2.setText("Familia");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        getContentPane().add(jLabel2, gridBagConstraints);

        jLabel3.setText("Codigo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel3, gridBagConstraints);

        jLabel4.setText("Codigos de barras");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel4, gridBagConstraints);

        jLabel5.setText("Codigo asociado");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel5, gridBagConstraints);

        jLabel6.setText("Detalle");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 54;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel6, gridBagConstraints);

        jLabel7.setText("Precio de Venta");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 17;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel7, gridBagConstraints);

        jLabel8.setText("Costo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel8, gridBagConstraints);

        jLabel10.setText("Margen");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 15;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel10, gridBagConstraints);

        txtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtId.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(20, 18, 0, 0);
        getContentPane().add(txtId, gridBagConstraints);

        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodigo.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtCodigo, gridBagConstraints);

        txtCodBarras1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodBarras1.setNextFocusableComponent(txtCodBarras2);
        txtCodBarras1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodBarras1MouseClicked(evt);
            }
        });
        txtCodBarras1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodBarras1KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtCodBarras1, gridBagConstraints);

        txtCodAsociado.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodAsociado.setNextFocusableComponent(txtCosto);
        txtCodAsociado.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodAsociadoMouseClicked(evt);
            }
        });
        txtCodAsociado.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodAsociadoKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtCodAsociado, gridBagConstraints);

        txtDetalle.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDetalle.setNextFocusableComponent(txtCodBarras1);
        txtDetalle.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDetalleMouseClicked(evt);
            }
        });
        txtDetalle.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDetalleKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 215;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtDetalle, gridBagConstraints);

        txtCodBarras2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodBarras2.setNextFocusableComponent(txtCodAsociado);
        txtCodBarras2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodBarras2MouseClicked(evt);
            }
        });
        txtCodBarras2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodBarras2KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
        getContentPane().add(txtCodBarras2, gridBagConstraints);

        cbFamilia.setNextFocusableComponent(txtDetalle);
        cbFamilia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbFamiliaKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(cbFamilia, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        bGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/guardar.png"))); // NOI18N
        bGuardar.setToolTipText("Guardar");
        bGuardar.setMinimumSize(new java.awt.Dimension(60, 60));
        bGuardar.setPreferredSize(new java.awt.Dimension(60, 60));
        bGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(bGuardar, gridBagConstraints);

        bEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/editar.png"))); // NOI18N
        bEditar.setToolTipText("Editar");
        bEditar.setMinimumSize(new java.awt.Dimension(60, 60));
        bEditar.setPreferredSize(new java.awt.Dimension(60, 60));
        bEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(bEditar, gridBagConstraints);

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eliminar.png"))); // NOI18N
        bBorrar.setToolTipText("Borrar");
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(bBorrar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridheight = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 20, 15);
        getContentPane().add(jPanel1, gridBagConstraints);

        txtCosto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txtCosto.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCosto.setText("0");
        txtCosto.setToolTipText("");
        txtCosto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCostoMouseClicked(evt);
            }
        });
        txtCosto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCostoKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.ipadx = 75;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtCosto, gridBagConstraints);

        txtPrecioVenta.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txtPrecioVenta.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtPrecioVenta.setText("0");
        txtPrecioVenta.setToolTipText("");
        txtPrecioVenta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtPrecioVentaMouseClicked(evt);
            }
        });
        txtPrecioVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPrecioVentaKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 17;
        gridBagConstraints.ipadx = 75;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtPrecioVenta, gridBagConstraints);

        txtMargen2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMargen2.setText("0%");
        txtMargen2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMargen2MouseClicked(evt);
            }
        });
        txtMargen2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMargen2KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 15;
        gridBagConstraints.ipadx = 75;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtMargen2, gridBagConstraints);

        jLabel9.setText("Localizacion");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 18;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 0, 0);
        getContentPane().add(jLabel9, gridBagConstraints);

        txtLocalizacion.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLocalizacion.setNextFocusableComponent(txtCosto);
        txtLocalizacion.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtLocalizacionMouseClicked(evt);
            }
        });
        txtLocalizacion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLocalizacionKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 18;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 18, 0, 0);
        getContentPane().add(txtLocalizacion, gridBagConstraints);

        jLabel11.setText("Igic");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 19;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(9, 31, 19, 0);
        getContentPane().add(jLabel11, gridBagConstraints);

        txtIgic.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0"))));
        txtIgic.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtIgic.setMinimumSize(new java.awt.Dimension(109, 20));
        txtIgic.setPreferredSize(new java.awt.Dimension(109, 20));
        txtIgic.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIgicMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(9, 18, 19, 0);
        getContentPane().add(txtIgic, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditarActionPerformed
        setActivarCampos(!txtDetalle.isEnabled());
    }//GEN-LAST:event_bEditarActionPerformed

    private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed
        setActivarCampos(false);
        String id = String.valueOf(Familias.getIdFamilia(String.valueOf(cbFamilia.getSelectedItem())));
        String [] datos = {txtDetalle.getText(),txtCodAsociado.getText(), 
                        txtCodBarras1.getText(), txtCodBarras2.getText(), txtLocalizacion.getText(),
                        txtCodigo.getText(),
                        String.valueOf(txtCosto.getValue()), txtMargen2.getText().replace("%", "").trim(), 
                        String.valueOf(txtPrecioVenta.getValue()),""+txtIgic.getValue(), id, txtId.getText()};
        Articulos.guardar (datos, guardar); 
        if (guardar) {
            String [] datos2 = {txtId.getText(), new Fecha().getFechaSQL(),"0"}; 
            Stock.guardar(datos2);
        }
        if (!guardar)
            dispose();
        else
            reset();
    }//GEN-LAST:event_bGuardarActionPerformed

    private void txtDetalleKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDetalleKeyPressed
        if (evt.getKeyCode() == 10) 
            txtCodBarras1.requestFocus();
    }//GEN-LAST:event_txtDetalleKeyPressed

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "¿Esta seguro de Borrar?","Borrar", JOptionPane.OK_CANCEL_OPTION)) {
            Stock.borrar(txtCodigo.getText());
            Articulos.borrar(txtId.getText());
            RegStock.borrar(txtId.getText());    //borro todo registro de stock de ese articulo
            reset();
        }
    }//GEN-LAST:event_bBorrarActionPerformed

    private void cbFamiliaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbFamiliaKeyPressed
        if (evt.getKeyCode() == 10) 
            txtDetalle.requestFocus();
    }//GEN-LAST:event_cbFamiliaKeyPressed

    private void txtCodBarras1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodBarras1KeyPressed
        if (evt.getKeyCode() == 10) 
            txtCodBarras2.requestFocus();
    }//GEN-LAST:event_txtCodBarras1KeyPressed

    private void txtCodBarras2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodBarras2KeyPressed
       if (evt.getKeyCode() == 10) 
            txtCodAsociado.requestFocus();
    }//GEN-LAST:event_txtCodBarras2KeyPressed

    private void txtCodAsociadoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodAsociadoKeyPressed
        if (evt.getKeyCode() == 10) 
            txtCosto.requestFocus();
    }//GEN-LAST:event_txtCodAsociadoKeyPressed

    private void txtCostoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCostoKeyPressed
        if (evt.getKeyCode() == 10)
            txtMargen2.requestFocus();
    }//GEN-LAST:event_txtCostoKeyPressed

    private void txtPrecioVentaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioVentaKeyPressed
        if (evt.getKeyCode() == 10) {
            txtLocalizacion.requestFocus();
            float costo = Float.parseFloat(txtCosto.getText().replace("€","").replace(",",".").trim());
            if (costo > 0) {
                float precio = Float.parseFloat(txtPrecioVenta.getText().replace("€","").replace(",",".").trim());
                float margen = (precio - costo) / costo * 100;
                DecimalFormat df = new DecimalFormat("#0.00");
                txtMargen2.setText(df.format(margen).replace(",", ".") +"%");
            }
        }
    }//GEN-LAST:event_txtPrecioVentaKeyPressed

    private void txtMargen2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMargen2KeyPressed
        if (evt.getKeyCode() == 10) {
            txtPrecioVenta.requestFocus();
            float costo = Float.parseFloat(txtCosto.getText().replace("€","").replace(",",".").trim());
            float margen = Float.parseFloat(txtMargen2.getText().replace("%", "").trim()) / 100;
            float precio = (1 + margen) * costo;
            txtPrecioVenta.setValue(Util.redondeo5(precio,2));
            txtMargen2.setText(txtMargen2.getText().replace("%", "") + "%");
        }
    }//GEN-LAST:event_txtMargen2KeyPressed

    private void txtLocalizacionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLocalizacionKeyPressed
        if (evt.getKeyCode() == 10)
            bGuardar.requestFocus();
    }//GEN-LAST:event_txtLocalizacionKeyPressed

    private void txtMargen2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMargen2MouseClicked
        txtMargen2.selectAll();
    }//GEN-LAST:event_txtMargen2MouseClicked

    private void txtDetalleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDetalleMouseClicked
        txtDetalle.selectAll();
    }//GEN-LAST:event_txtDetalleMouseClicked

    private void txtCodBarras1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodBarras1MouseClicked
        txtCodBarras1.selectAll();
    }//GEN-LAST:event_txtCodBarras1MouseClicked

    private void txtCodBarras2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodBarras2MouseClicked
        txtCodBarras2.selectAll();
    }//GEN-LAST:event_txtCodBarras2MouseClicked

    private void txtCodAsociadoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodAsociadoMouseClicked
        txtCodAsociado.selectAll();
    }//GEN-LAST:event_txtCodAsociadoMouseClicked

    private void txtCostoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCostoMouseClicked
        txtCosto.selectAll();
    }//GEN-LAST:event_txtCostoMouseClicked

    private void txtPrecioVentaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPrecioVentaMouseClicked
        txtPrecioVenta.selectAll();
    }//GEN-LAST:event_txtPrecioVentaMouseClicked

    private void txtLocalizacionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtLocalizacionMouseClicked
        txtLocalizacion.selectAll();
    }//GEN-LAST:event_txtLocalizacionMouseClicked

    private void txtIgicMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIgicMouseClicked
        txtIgic.selectAll();
    }//GEN-LAST:event_txtIgicMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FArticulo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bEditar;
    private javax.swing.JButton bGuardar;
    private javax.swing.JComboBox cbFamilia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtCodAsociado;
    private javax.swing.JTextField txtCodBarras1;
    private javax.swing.JTextField txtCodBarras2;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JFormattedTextField txtCosto;
    private javax.swing.JTextField txtDetalle;
    private javax.swing.JTextField txtId;
    private javax.swing.JFormattedTextField txtIgic;
    private javax.swing.JTextField txtLocalizacion;
    private javax.swing.JTextField txtMargen2;
    private javax.swing.JFormattedTextField txtPrecioVenta;
    // End of variables declaration//GEN-END:variables
}
